package com.zang.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author gs
 * @date 2022/6/13 4:14 PM
 */
public class DateFormatUtil {

    public static final String SDF_YMD = "yyyy-MM-dd";
    public static final String SDF_YMD_N = "yyyyMMdd";
    public static final String SDF_YMD_HMS_24 = "yyyy-MM-dd HH:mm:ss";
    public static final String SDF_YMD_HMS_24_N = "yyyyMMddHHmmss";
    public static final String SDF_HMS = "HH-mm-ss";

    public static String format(Date date, String pattern) {
        return (new SimpleDateFormat(pattern)).format(date);
    }

    /**
     * 获取当天0点时间
     * @param date 当天时间
     * @return 当天0点 00:00:00
     */
    public static String formatDayZeroTime(Date date) {
        return (new SimpleDateFormat(SDF_YMD)).format(date) + " 00:00:00";
    }

    /**
     * 获取当天0点时间-无间隔符
     * @param date 当天时间
     * @return 当天0点 00:00:00
     */
    public static String formatDayZeroTimeN(Date date) {
        return (new SimpleDateFormat(SDF_YMD_N)).format(date) + "000000";
    }

    /**
     * 获取当前结束时间
     * @param date 当天时间
     * @return 当天结束时间 23:59:59
     */
    public static String formatDayEndTime(Date date) {
        return (new SimpleDateFormat(SDF_YMD)).format(date) + " 23:59:59";
    }

    /**
     * 获取当前结束时间-无间隔符
     * @param date 当天时间
     * @return 当天结束时间 23:59:59
     */
    public static String formatDayEndTimeN(Date date) {
        return (new SimpleDateFormat(SDF_YMD_N)).format(date) + "235959";
    }

    /**
     * 获取昨天0点时间
     * 2022-06-12 00:00:00
     * @return 昨天0点 00:00:00
     */
    public static String formatYesterdayZeroTime() {
        Calendar start = Calendar.getInstance();
        start.add(Calendar.DAY_OF_MONTH, -1);
        return formatDayZeroTime(start.getTime());
    }

    /**
     * 获取昨天结束时间
     * 2022-06-12 23:59:59
     * @return 昨天 23:59:59
     */
    public static String formatYesterdayEndTime() {
        Calendar start = Calendar.getInstance();
        start.add(Calendar.DAY_OF_MONTH, -1);
        return formatDayEndTime(start.getTime());
    }


}
